
cc.Class({
    extends: require('FrameComponent'),

    properties: {
        isTrack: false
    },

    onLoad(){
        // this._super();
        this.checkTime = 0;
        this.trackTarget = null;
    },

    startTrack(){
        
        //开始追踪
        this.isTrack = true;
        if(this.trackTarget){
            this.onTargetDestroy();
        }
    },

    onEnable(){

    },
    onDisable(){

    },

    start(){

    },

    setCamp(camp){
        this.camp = camp;
    },

    updateTrack(){
        this.camp=this.camp?this.camp:1
        if(this.camp == global.Enum.CAMP.neutral){
            // console.log('中立生物不允许跟踪')
            this.randomTrack(global.GameLoop.GameMgr.colliderMgr.getGroup(global.Enum.CAMP.friend))
        } else if(this.camp == global.Enum.CAMP.friend){
            this.randomTrack(global.GameLoop.GameMgr.colliderMgr.getGroup(global.Enum.CAMP.enemy))
        } else {
            // this.randomTrack(global.GameLoop.GameMgr.colliderMgr.getGroup(global.Enum.CAMP.friend))
        }
    },
    randomTrack(array){
        var arr = [];
        for(var i=0; i<array.length; i++){ //过滤还在无敌的
            var ign = array[i]
            if(!ign.isIgnore){
                var no = ign.node;
                if(global.Common.pointInScreen(no)){
                    arr.push(array[i])
                }
            }
        }
        if(arr.length > 0){
            var ridx = Math.floor(Math.random() * arr.length);
            var obj = arr[ridx].node;
            this.trackTarget = obj;
            this.trackTarget.on('child-removed',this.onTargetDestroy,this)
            this.trackTarget.on('monster-ignore',this.onTargetDestroy,this)

            // cc.log('寻找到目标:',obj.name,obj.getPosition());
            this.o_particleSprite && (this.o_particleSprite.enabled = false);
        } else if(this.o_particleSprite){
            this.o_particleSprite.enabled = true;
            // this.o_particleSprite.node._particle = this.node._particle;
        }
    },
    onTargetDestroy(){ //目标已经死亡，寻找新的目标
        this.trackTarget.off('child-removed',this.onTargetDestroy,this)
        this.trackTarget.off('monster-ignore',this.onTargetDestroy,this)

        this.trackTarget = null;
        // if(global.GameLoop.GameMgr){
        //     this.updateTrack();
        // }
    },

    updateMove(dt){
        // if(this.camp == global.Enum.CAMP.friend){
            this.node._particle.life -=dt;
            if(this.node._particle.life < 0){
                // this.destroy();
                this.o_particleSprite.recovery();
                return;
            }
            var p1 = this.node.getPosition();
            var p2 = this.trackTarget.getPosition();
            var len =this.node._particle.speedInit * dt*1.3
            var a = global.Common.getAngle(p1,p2);
            var ca = Math.cos(a), sa = Math.sin(a);
            var dx = ca*len*(p2.x>p1.x?1:-1)+p1.x;
            var dy = sa*len*(p2.y>p1.y?1:-1)+p1.y;
            
            this.node.x = dx;
            this.node.y = dy;
    },

    updateStep(dt){
        if(!this.o_particleSprite){
            this.o_particleSprite = this.node.getComponent('ParticleSprite')
            this.node._particle = this.o_particleSprite.node._particle;
            return;
        }
        if(this.isTrack){
            if(this.trackTarget){
                this.updateMove(dt)
                return;
            }
            this.checkTime -= dt
            if(this.checkTime < 0){
                this.updateTrack();
            }
        } 
    },

    update(dt){
        if(cc.isPauseGame){
            return;
        }
       this.autoUpdate && this.updateStep(dt)
    },
    gameUpdate(dt){
        this.updateStep(dt)
    }



});
